package com.hunliji.monitor.web.facade.impl;

import com.hunliji.monitor.service.api.TestService;
import com.hunliji.monitor.service.api.model.bo.TestBO;
import com.hunliji.monitor.share.component.ComponentLocator;
import com.hunliji.monitor.share.component.lock.DistributedLockFacade;
import com.hunliji.monitor.share.component.mq.rocket.RocketMqFacade;
import com.hunliji.monitor.share.component.mq.rocket.constants.RocketMqConstants;
import com.hunliji.monitor.web.facade.TestFacade;
import com.hunliji.monitor.web.model.vo.TestVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author xingfeng
 * @date 2018/11/26
 */
@Component(value = "testFacade")
public class TestFacadeImpl implements TestFacade {
    private static final Logger logger = LoggerFactory.getLogger(TestFacadeImpl.class);
    @Resource
    private DistributedLockFacade distributedLockFacade;
    @Resource
    private TestService testService;
    @Resource
    private RocketMqFacade rocketMqFacade;

    @Override
    public TestVO getById(Long id) {
        TestBO bo = testService.getById(id);
        return ComponentLocator.getConverter().convert(bo, TestVO.class);
    }

    @Override
    public void testDistributeLock() {
        Boolean oneSuccess = distributedLockFacade.lock("aaa",50L,200000L);
        Boolean twoSuccess = distributedLockFacade.lock("aaa",50L,200000L);
        Boolean threeSuccess = distributedLockFacade.lock("bbb",50L,200000L);
        logger.info("打印锁的加锁结果，oneSuccess:{},twoSuccess:{},threeSuccess:{}",oneSuccess,twoSuccess,threeSuccess);
    }

    @Override
    public void testLog() {
        //LoggerUtil.info("测试，aaa:{},bbb:{}",loggerConfig.getDebug(),"aaaa",1);
        logger.debug("aaa");
    }

    @Override
    public void testMq() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        rocketMqFacade.send(RocketMqConstants.TEST_TOPIC,RocketMqConstants.TEST_TAG,"测试同步消息");
        logger.info("测试同步消息发送时间:{}",sdf.format(new Date()));


        rocketMqFacade.delayedSend(RocketMqConstants.TEST_TOPIC,RocketMqConstants.TEST_TAG,"测试延时1分钟的消息",RocketMqConstants.DelayLevel.ONE_MINUTE);
        logger.info("测试延时1分钟的消息:{}",sdf.format(new Date()));

        rocketMqFacade.delayedSend(RocketMqConstants.TEST_TOPIC,RocketMqConstants.TEST_TAG,"测试延时5分钟的消息",RocketMqConstants.DelayLevel.FIVE_MINUTE);
        logger.info("测试延时5分钟的消息:{}",sdf.format(new Date()));
    }

    @Override
    public void testMqBatch() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        rocketMqFacade.send(RocketMqConstants.TEST_TOPIC,RocketMqConstants.TEST_TAG_BATCH,"测试同步消息1");
        rocketMqFacade.send(RocketMqConstants.TEST_TOPIC,RocketMqConstants.TEST_TAG_BATCH,"测试同步消息2");
        rocketMqFacade.send(RocketMqConstants.TEST_TOPIC,RocketMqConstants.TEST_TAG_BATCH,"测试同步消息3");
        rocketMqFacade.send(RocketMqConstants.TEST_TOPIC,RocketMqConstants.TEST_TAG_BATCH,"测试同步消息4");
        rocketMqFacade.send(RocketMqConstants.TEST_TOPIC,RocketMqConstants.TEST_TAG_BATCH,"测试同步消息5");
        logger.info("测试同步消息发送时间:{}",sdf.format(new Date()));

    }

    @Override
    public void testMqOrder() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        try {
            logger.info("business 测试顺序消息_start:{}",sdf.format(new Date()));
            rocketMqFacade.orderSend(RocketMqConstants.TEST_TOPIC,RocketMqConstants.TEST_TAG_ORDER,"测试顺序消息1","测试");
            rocketMqFacade.orderSend(RocketMqConstants.TEST_TOPIC,RocketMqConstants.TEST_TAG_ORDER,"测试顺序消息2","测试");
            rocketMqFacade.orderSend(RocketMqConstants.TEST_TOPIC,RocketMqConstants.TEST_TAG_ORDER,"测试顺序消息3","测试1");
            logger.info("business 测试顺序消息_end:{}",sdf.format(new Date()));
        }catch (Exception e){
            logger.error("business 测试顺序消息发生异常",e);
        }
    }
}
